package com.toivan.mt.views;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;
import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.toivan.mt.R;
import com.toivan.mt.model.MtBeautyMode;
import com.toivan.mt.model.MtSharedPrefKey;
import com.toivan.mt.model.RxBusAction;
import com.toivan.mt.utils.MtJsonParser;
import com.toivan.mt.utils.MtSharedPreferences;
import com.toivan.sdk.MtSDK;

public class MtBeautyPanel extends ConstraintLayout {
    private MtSDK mtSDK;
    private ImageView beautyIV;
    private MtCuteClassView cuteClassView;
    private MtBeautyView beautyView;

    private FrameLayout beautyModeContainer;
    private View beauty;
    private View faceTrim;
    private View cute;
    private View filter;
    private View quickBeauty;
    private TextView interactionHintTV;
    private View indicatorLL;
    private View beautyLL;
    private ImageView btnBack;
    private View btnBottomContainer;
    private View btnBackBottom;
    private RecyclerView cuteRV;

    private boolean isRatioFull = true;
    private String interactionHint = MtSharedPrefKey.interactionHint;

    public static boolean isCuteClassVisible = false;

    public MtBeautyPanel(Context context) {
        super(context);
    }

    public MtBeautyPanel(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MtBeautyPanel(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MtBeautyPanel init(@NonNull MtSDK mtSDK) {
        this.mtSDK = mtSDK;

        MtSharedPreferences.getInstance().init(getContext(), mtSDK);

        RxBus.get().register(this);

        checkVersion();


        initView();

        initData();

        return this;
    }

    private void checkVersion() {
        String curVersion = MtSharedPreferences.getInstance().getUiVersion();
        String newVersion = com.toivan.mt.BuildConfig.MT_VERSION_NAME;
        if (curVersion.equals("") || Float.parseFloat(newVersion) > Float.parseFloat(curVersion)) {
            MtJsonParser.getInstance().resetFile(getContext());
            MtSharedPreferences.getInstance().setUiVersion(newVersion);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();

        RxBus.get().unregister(this);
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_beauty_panel, this);

        beautyIV = findViewById(R.id.beautyIV);

        cuteClassView = findViewById(R.id.cute_class_view);
        beautyView = findViewById(R.id.mtBeautyView);

        beautyModeContainer = findViewById(R.id.beautyModeContainer);
        beauty = findViewById(R.id.beauty);
        faceTrim = findViewById(R.id.face_trim);
        cute = findViewById(R.id.cute);
        filter = findViewById(R.id.filter);
        quickBeauty = findViewById(R.id.quick_beauty);
        interactionHintTV = findViewById(R.id.interaction_hint);
    }

    private void initData() {

        cuteClassView.init();
        beautyView.init(mtSDK);

        indicatorLL = beautyView.findViewById(R.id.indicatorLL);
        beautyLL = beautyView.findViewById(R.id.beautyLL);
        btnBack = beautyView.findViewById(R.id.btn_back);
        btnBottomContainer = beautyView.findViewById(R.id.btn_bottom);
        btnBackBottom = beautyView.findViewById(R.id.btn_back_2);
        cuteRV = cuteClassView.findViewById(R.id.cute_recyclerview);

        setRatioFull(true);

        MtSharedPreferences.getInstance().initAllSPValues(getContext());
        showHint(interactionHint);


        beautyIV.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (beautyModeContainer != null && beautyModeContainer.getVisibility() == GONE) {
                    beautyModeContainer.setVisibility(VISIBLE);
                }
                if (beautyIV != null) {
                    beautyIV.setVisibility(GONE);
                }
                //todo bus hide btn capture
                RxBus.get().post("HIDE_BTN_CAPTURE", true);
            }
        });

        beauty.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                loadBeautyAndShapeView(MtBeautyMode.MODE_BEAUTY);
            }
        });

        faceTrim.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                loadBeautyAndShapeView(MtBeautyMode.MODE_FACE_SHAPE);
            }
        });

        cute.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                loadCuteView();
            }
        });

        filter.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                beautyView.mtBarView.hideSeekBar();
                loadFilterAndQuickView(MtBeautyMode.MODE_FILTER);
            }
        });

        quickBeauty.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                beautyView.mtBarView.hideSeekBar();
                loadFilterAndQuickView(MtBeautyMode.MODE_QUICK_BEAUTY);
            }
        });

        btnBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                doHideBeautyView();
            }
        });

        btnBackBottom.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (beautyView != null && beautyView.getVisibility() == VISIBLE) {
                    beautyView.setVisibility(GONE);
                    showBeautyModeContainer();
                }
            }
        });

        //空白处隐藏面板
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (event.getAction() == MotionEvent.ACTION_MOVE || event.getAction() == MotionEvent.ACTION_UP) {
                    return false;
                }

                v.performClick();
                if (beautyView != null && beautyView.getVisibility() == VISIBLE) {
                    doHideBeautyView();
                    return true;
                } else if (cuteClassView != null && cuteClassView.getVisibility() == VISIBLE) {
                    cuteClassView.setVisibility(GONE);
                    showBeautyModeContainer();
                    return true;
                } else if (beautyModeContainer != null && beautyModeContainer.getVisibility() == VISIBLE) {
                    beautyModeContainer.setVisibility(GONE);
                    if (beautyIV != null) {
                        beautyIV.setVisibility(VISIBLE);
                    }
                    //todo bus show btn capture
                    RxBus.get().post("HIDE_BTN_CAPTURE", false);
                    return true;
                }

                return false;
            }
        });
    }

    //加载萌图
    public void loadCuteView() {
        if (beautyView != null && beautyView.getVisibility() == GONE) {
            hideBtnBottomContainer();
            changeBeautyViewBackBlack();
            hideBeautyModeContainer();
        }
        showCuteClassView();
    }

    //加载滤镜，一键美颜
    public void loadFilterAndQuickView(int modeQuickBeauty) {
        beautyView.refreshData(modeQuickBeauty);
        if (beautyView != null && beautyView.getVisibility() == GONE) {
            hideBtnBottomContainer();
            changeBeautyViewBackWhite();
            beautyView.setVisibility(VISIBLE);
            hideBeautyModeContainer();
        }
    }

    //加载美颜，美型
    public void loadBeautyAndShapeView(int modeBeauty) {
        beautyView.refreshData(modeBeauty);
        if (beautyView != null && beautyView.getVisibility() == GONE) {
            showBtnBottomContainer();
            changeBeautyViewBackWhite();
            beautyView.setVisibility(VISIBLE);
            hideBeautyModeContainer();
        }
    }


    public void setRatioFull(boolean ratioFull) {
        isRatioFull = ratioFull;
        changeAllViewsBack();
    }

    @Subscribe(thread = EventThread.MAIN_THREAD,
               tags = {
                   @Tag(RxBusAction.ACTION_SHOW_INTERACTION)
               })
    public void showHint(String hint) {
        MtSharedPrefKey.interactionHint = hint;
        interactionHintTV.setVisibility(TextUtils.isEmpty(hint) ? View.GONE : View.VISIBLE);
        interactionHintTV.setText(hint);
    }

    @Subscribe(thread = EventThread.MAIN_THREAD,
               tags = {
                   @Tag(RxBusAction.ACTION_CUTE_CLASS_SELECTED)
               })
    public void selectCuteClass(Integer position) {
        //        hideBtnBottomContainer();
        changeBeautyViewBackBlack();

        switch (position) {
            case 0:
                beautyView.refreshCuteData(MtBeautyMode.MODE_STICKER);
                break;
            case 1:
                beautyView.refreshCuteData(MtBeautyMode.MODE_EXPRESSION);
                break;
            case 2:
                beautyView.refreshCuteData(MtBeautyMode.MODE_MASK);
                break;
            case 3:
                beautyView.refreshCuteData(MtBeautyMode.MODE_ATMOSPHERE);
                break;
            case 4:
                beautyView.refreshCuteData(MtBeautyMode.MODE_WATERMARK);
                break;
            case 5:
                beautyView.refreshCuteData(MtBeautyMode.MODE_PORTRAIT);
                break;
            case 6:
                beautyView.refreshCuteData(MtBeautyMode.MODE_GREEN_SCREEBN);
                break;
        }
        if (beautyView != null) {
            hideCuteClassView();
            beautyView.setVisibility(VISIBLE);
        }
    }

    private void doHideBeautyView() {
        beautyView.setVisibility(GONE);
        if (isCuteClassVisible) {
            isCuteClassVisible = false;
            showCuteClassView();
        } else {
            showBeautyModeContainer();
        }
    }

    private void showBeautyModeContainer() {
        if (beautyModeContainer != null && beautyModeContainer.getVisibility() != VISIBLE) {
            beautyModeContainer.setVisibility(VISIBLE);
        }
    }

    private void hideBeautyModeContainer() {
        if (beautyModeContainer != null && beautyModeContainer.getVisibility() == VISIBLE) {
            beautyModeContainer.setVisibility(GONE);
        }
    }

    private void showCuteClassView() {
        if (cuteClassView != null && cuteClassView.getVisibility() != VISIBLE) {
            cuteClassView.setVisibility(VISIBLE);
        }
    }

    private void hideCuteClassView() {
        if (cuteClassView != null && cuteClassView.getVisibility() == VISIBLE) {
            cuteClassView.setVisibility(GONE);
        }
    }

    private void showBtnBottomContainer() {
        if (btnBottomContainer != null && btnBack != null) {
            btnBottomContainer.setVisibility(View.VISIBLE);
            btnBack.setVisibility(GONE);
        }
    }

    private void hideBtnBottomContainer() {
        if (btnBottomContainer != null && btnBack != null) {
            btnBottomContainer.setVisibility(View.GONE);
            btnBack.setVisibility(VISIBLE);
        }
    }

    private void changeBeautyViewBackWhite() {
        if (indicatorLL != null && beautyLL != null && btnBack != null && btnBottomContainer != null) {
            if (isRatioFull) {
                indicatorLL.setBackgroundColor(getResources().getColor(R.color.bg_panel_80_transparent));
                beautyLL.setBackgroundColor(getResources().getColor(R.color.bg_panel_80_transparent));
                btnBottomContainer.setBackgroundColor(getResources().getColor(R.color.bg_panel_80_transparent));
            } else {
                indicatorLL.setBackgroundColor(getResources().getColor(R.color.bg_panel_tab));
                beautyLL.setBackgroundColor(getResources().getColor(R.color.bg_panel_content));
                btnBottomContainer.setBackgroundColor(getResources().getColor(R.color.bg_panel_content));
            }
            btnBack.setImageResource(R.drawable.icon_back_black);
        }
    }

    private void changeBeautyViewBackBlack() {
        if (indicatorLL != null && beautyLL != null && btnBack != null) {
            if (isRatioFull) {
                indicatorLL.setBackgroundColor(getResources().getColor(R.color.bg_tab_cute_70_transparent));
                beautyLL.setBackgroundColor(getResources().getColor(R.color.bg_tab_cute_70_transparent));
            } else {
                indicatorLL.setBackgroundColor(getResources().getColor(R.color.bg_tab_cute));
                beautyLL.setBackgroundColor(getResources().getColor(R.color.bg_tab_cute));
            }
            btnBack.setImageResource(R.drawable.icon_back_white);
        }
    }

    private void changeAllViewsBack() {
        if (beautyModeContainer == null || cuteRV == null || indicatorLL == null || beautyLL == null || btnBottomContainer == null) {
            return;
        }
        if (isRatioFull) {
            beautyModeContainer.setBackgroundColor(getResources().getColor(R.color.bg_panel_80_transparent));
            cuteRV.setBackgroundColor(getResources().getColor(R.color.bg_panel_80_transparent));
            if (isCuteClassVisible) {
                indicatorLL.setBackgroundColor(getResources().getColor(R.color.bg_tab_cute_70_transparent));
                beautyLL.setBackgroundColor(getResources().getColor(R.color.bg_tab_cute_70_transparent));
            } else {
                indicatorLL.setBackgroundColor(getResources().getColor(R.color.bg_panel_80_transparent));
                beautyLL.setBackgroundColor(getResources().getColor(R.color.bg_panel_80_transparent));
                btnBottomContainer.setBackgroundColor(getResources().getColor(R.color.bg_panel_80_transparent));
            }
        } else {
            beautyModeContainer.setBackgroundColor(getResources().getColor(R.color.bg_panel_content));
            cuteRV.setBackgroundColor(getResources().getColor(R.color.bg_panel_content));
            if (isCuteClassVisible) {
                indicatorLL.setBackgroundColor(getResources().getColor(R.color.bg_tab_cute));
                beautyLL.setBackgroundColor(getResources().getColor(R.color.bg_tab_cute));
            } else {
                indicatorLL.setBackgroundColor(getResources().getColor(R.color.bg_panel_tab));
                beautyLL.setBackgroundColor(getResources().getColor(R.color.bg_panel_content));
                btnBottomContainer.setBackgroundColor(getResources().getColor(R.color.bg_panel_content));
            }
        }
    }

}
